﻿using System;
using System.Runtime.InteropServices;

namespace MediaRemoteServer
{
    public class MediaListPlayer : IDisposable
    {
        #region libvlc functions

        [DllImport("libvlc")]
        private static extern IntPtr libvlc_media_list_player_new(IntPtr p_libvlc_instance, IntPtr p_eexception);

        [DllImport("libvlc")]
        private static extern void libvlc_media_list_player_release(IntPtr p_mlp);

        [DllImport("libvlc")]
        private static extern void libvlc_media_list_player_set_media_list(IntPtr p_mlp, IntPtr p_mlist, IntPtr p_e);

        [DllImport("libvlc")]
        private static extern void libvlc_media_list_player_play(IntPtr p_mlp, IntPtr p_e);

        [DllImport("libvlc")]
        private static extern void libvlc_media_list_player_pause(IntPtr p_mlp, IntPtr p_e);

        [DllImport("libvlc")]
        private static extern int libvlc_media_list_player_is_playing(IntPtr p_mlp, IntPtr p_e);

        [DllImport("libvlc")]
        private static extern libvlc_state_t libvlc_media_list_player_get_state(IntPtr p_mlp, IntPtr p_e);

        [DllImport("libvlc")]
        private static extern void libvlc_media_list_player_play_item_at_index(IntPtr p_mlp, int i_index, IntPtr p_e);

        [DllImport("libvlc")]
        private static extern void libvlc_media_list_player_stop(IntPtr p_mlp, IntPtr p_e);

        [DllImport("libvlc")]
        private static extern void libvlc_media_list_player_next(IntPtr p_mlp, IntPtr p_e);

        [DllImport("libvlc")]
        private static extern void libvlc_media_list_player_previous(IntPtr p_mlp, IntPtr p_e);

        [DllImport("libvlc")]
        private static extern void libvlc_media_list_player_set_playback_mode(IntPtr p_mlp, libvlc_playback_mode_t e_mode, IntPtr p_e);

        [DllImport("libvlc")]
        private static extern void libvlc_media_list_player_play_item(IntPtr p_mlp, IntPtr p_m, IntPtr p_e);

        #endregion

        #region libvlc data types

        public enum libvlc_state_t
        {
            libvlc_NothingSpecial,
            libvlc_Opening,
            libvlc_Buffering,
            libvlc_Playing,
            libvlc_Paused,
            libvlc_Stopped,
            libvlc_Ended,
            libvlc_Error,
        }

        public enum libvlc_playback_mode_t
        {
            libvlc_playback_mode_default,
            libvlc_playback_mode_loop,
            libvlc_playback_mode_repeat,
        }

        #endregion

        private IntPtr instance;
        private LibVLC libVLC;

        public MediaListPlayer(LibVLC libVLCInstance)
        {
            libVLC = libVLCInstance;

            libVLC.clearException();
            instance = libvlc_media_list_player_new(libVLC.instance, libVLC.pException);
        }

        public void setMediaList(MediaList playlist)
        {
            libVLC.clearException();
            libvlc_media_list_player_set_media_list(instance, playlist.instance, libVLC.pException);
        }

        public void play()
        {
            libVLC.clearException();
            libvlc_media_list_player_play(instance, libVLC.pException);
        }

        public void pause()
        {
            libVLC.clearException();
            libvlc_media_list_player_pause(instance, libVLC.pException);
        }

        public bool isPlaying()
        {
            libVLC.clearException();
            return libvlc_media_list_player_is_playing(instance, libVLC.pException) != 0;
        }

        public libvlc_state_t getState()
        {
            libVLC.clearException();
            return libvlc_media_list_player_get_state(instance, libVLC.pException);
        }

        public void playItemAtIndex(int index)
        {
            libVLC.clearException();
            libvlc_media_list_player_play_item_at_index(instance, index, libVLC.pException);
        }

        public void playItem(MediaItem item)
        {
            libVLC.clearException();
            libvlc_media_list_player_play_item(instance, item.instance, libVLC.pException);
        }

        public void stop()
        {
            libVLC.clearException();
            libvlc_media_list_player_stop(instance, libVLC.pException);
        }

        public void next()
        {
            libVLC.clearException();
            libvlc_media_list_player_next(instance, libVLC.pException);
        }

        public void previous()
        {
            libVLC.clearException();
            libvlc_media_list_player_previous(instance, libVLC.pException);
        }

        public void setPlaybackMode(libvlc_playback_mode_t mode)
        {
            libVLC.clearException();
            libvlc_media_list_player_set_playback_mode(instance, mode, libVLC.pException);
        }

        #region IDisposable Members

        public void Dispose()
        {
            libvlc_media_list_player_release(instance);
        }

        #endregion
    }
}
